package com.mine.dzq.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.OSSObject;
import com.mine.dzq.service.StorageService;

import java.io.InputStream;

/**
 * @Author Pudding
 * OSS存储实现类
 * @Date 2025/4/7 10:52
 */
public class OSSStorageService implements StorageService {
    private final OSS ossClient;
    private final String bucketName;

    public OSSStorageService(OSS ossClient, String bucketName) {
        this.ossClient = ossClient;
        this.bucketName = bucketName;
    }

    @Override
    public void upload(String objectKey, InputStream input) {
        try {
            ossClient.putObject(bucketName, objectKey, input);
        } catch (Exception e) {
            throw new RuntimeException("上传文件到OSS失败", e);
        }
    }

    @Override
    public InputStream download(String objectKey) {
        try {
            OSSObject ossObject = ossClient.getObject(bucketName, objectKey);
            return ossObject.getObjectContent();
        } catch (Exception e) {
            throw new RuntimeException("从OSS下载文件失败", e);
        }
    }

    @Override
    public void delete(String objectKey) {
        try {
            ossClient.deleteObject(bucketName, objectKey);
        } catch (Exception e) {
            throw new RuntimeException("删除OSS文件失败", e);
        }
    }
}